package deliciouse.servlet;

import java.io.IOException;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import deliciouse.biz.UserBiz;
import deliciouse.biz.impl.UserBizImpl;
import deliciouse.entity.Users;

@ WebServlet("/CheckRegister")
public class RegisterCheckServlet extends HttpServlet {
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String userName = request.getParameter("username");
		String password = request.getParameter("password");
		Date lastLoginTime = new Date(0);
		UserBiz userbiz = new UserBizImpl();
		Users user = new Users();
		user.setUserName(userName);
		user.setPassword(password);
		user.setLastLoginTime(lastLoginTime);
		if (userbiz.userIsExist(userName).getUserName()!=null) {
			request.setAttribute("erro", "用户已经存在！");
			request.getRequestDispatcher("register.jsp").forward(request, response);

		} else {
			userbiz.addUser(user);
			response.sendRedirect("login.jsp");
		}
	}
}
